package com.fili.gui;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.InvocationTargetException;

/**
 * Date: Oct 30, 2005
 * Time: 2:48:37 PM
 *
 * @author Claudiu-Florin FILIMON for Borux Consulting S.R.L.
 */
public class SplashLoader extends Window implements MouseListener
{
    private Image img;
/*-------------------------------------------------------------------------------------
    Getters and setters
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
	Constructors
 --------------------------------------------------------------------------------------*/

    private SplashLoader(Frame owner, String imgPath, boolean disposable)
    {
        super(owner);
        img = loadImage(imgPath);

        // Center the window on the screen
        int imgWidth = img.getWidth(this);
        int imgHeight = img.getHeight(this);
        Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((scrSize.width - imgWidth) / 2, (scrSize.height - imgHeight) / 2, imgWidth, imgHeight);

        if (disposable)
            addMouseListener(this);

        setAlwaysOnTop(true);

        setVisible(true);
    }


/*-------------------------------------------------------------------------------------
    Methods
 --------------------------------------------------------------------------------------*/

    private Image loadImage(String imgPath)
    {
//		URL imageURL = SplashLoader.class.getResource(imgPath);
        Image img = Toolkit.getDefaultToolkit().createImage(imgPath);
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(img, 0);
        try
        {
            mt.waitForID(0);
        }
        catch (InterruptedException ie)
        {
            return null;
        }

        return img;
    }

    public static boolean showSplash(Frame owner, String imgPath)
    {
        new SplashLoader(owner, imgPath, true);
        return true;
    }

    public static boolean showSplash(String imgPath, String className)
    {
        return showSplash(imgPath, className, "main", null);
    }

    public static boolean showSplash(String imgPath, String className, String methodName)
    {
        return showSplash(imgPath, className, methodName, null);
    }

    public static boolean showSplash(String imgPath, String className, String methodName, String args[])
    {
        SplashLoader sl = new SplashLoader(new Frame(), imgPath, false);
        try
        {
            Class.forName(className).getMethod(methodName, new Class[]{String[].class}).invoke(null, new Object[]{args});
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        catch (NoSuchMethodException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        sl.dispose();
        return true;
    }
/*-------------------------------------------------------------------------------------
    Overrides
 --------------------------------------------------------------------------------------*/

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        g.drawImage(img, 0, 0, null);
    }

/*-------------------------------------------------------------------------------------
    Implementations
 --------------------------------------------------------------------------------------*/

    public void mouseClicked(MouseEvent e)
    {
        if (e.getButton() == MouseEvent.BUTTON1)
        {
            dispose();
        }
    }

    public void mousePressed(MouseEvent e)
    {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseReleased(MouseEvent e)
    {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseEntered(MouseEvent e)
    {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseExited(MouseEvent e)
    {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}